var layer = layui.layer
    , table = layui.table
    , $ = layui.jquery,
    form = layui.form,
    id = "",
    lx = 67;
layui.use(['tableEdit', "excel"], function () {
    let layer = layui.layer,
        excel = layui.excel;

    table.render({
        elem: '#categoryId'
        , type: "GET"
        , url: "product/getDictionaryListByPage"
        , where: {"lx": lx, "name": $('#names').val()}
        , toolbar: '#toolbarDemo'
        , cols: [[
            {checkbox: true, fixed: true}
            , {field: 'code', title: '编码', sort: true}
            , {field: 'name', title: '名称', sort: true}
            , {
                field: 'valid', title: '是否停用', sort: true, templet: function (d) {
                    return d.valid == 1 ? "否" : "是";
                }
            }
            , {field: 'remark', title: '中文描述', sort: true}
            , {field: 'englishname', title: '英文描述', sort: true}
        ]]
        , id: 'categoryTableId'
        , height: $(document).height() - $('#categoryId').offset().top - 50
        , page: true
    });

    table.on('toolbar(categoryTable)', function (obj) {
        let checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
            case 'newData':
                layer.open({
                    type: 1,
                    title: "新增颜色订货号",
                    area: ['685px', '600px'],
                    content: $("#fromEditor")//引用的弹出层的页面层的方式加载修改界面表单
                });
                //清空表单
                $('#editor-form')[0].reset();
                //获取最新编码
                getNewCode();
                //默认有效
                $("#valid").val(1);
                form.render();
                //新增 主键置空
                id = "";
                break;
            case 'edit':
                if (checkStatus.data.length == 0) {
                    layer.msg("请选择一条数据！");
                    return false;
                }
                layer.open({
                    type: 1,
                    title: "修改颜色订货号",
                    area: ['685px', '600px'],
                    content: $("#fromEditor")//引用的弹出层的页面层的方式加载修改界面表单
                });
                //给表单赋值
                form.val("editor-form", {
                    "code": checkStatus.data[0].code,
                    "name": checkStatus.data[0].name,
                    "valid": checkStatus.data[0].valid,
                    "englishname": checkStatus.data[0].englishname,
                    "remark": checkStatus.data[0].remark
                })
                //修改 主键赋值
                id = checkStatus.data[0].id;
                break
            case 'delete':
                if (checkStatus.data.length == 0) {
                    layer.msg("请选择一条数据！");
                    return false;
                }
                let data = checkStatus.data;
                let delIds = "";
                for (let i in data) {
                    if (delIds.length == 0) {
                        delIds = delIds + data[i].id;
                    } else {
                        delIds = delIds + "," + data[i].id;
                    }
                }
                if (batchDelete(delIds)) {
                    layer.msg("停用成功");
                    table.reload('categoryTableId', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    }, 'data');
                } else {
                    layer.msg("停用失败");
                }
                break;
            case 'search':
                let names = $('#names').val();
                table.reload('categoryTableId', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {"lx": lx, "name": names}
                }, 'data');
                break;
            case 'exportExcel':
                exportExcel();
                break;
        }
        ;
    });


    //导出Excel
    function exportExcel() {
        let index = layer.load(2, {
            shade: [0.4, '#000'],
            content: '加载中...',
            success: function (layero) {
                layero.find('.layui-layer-content').css({
                    'padding-top': '40px',
                    'width': '200px'
                });
            }
        });
        $.ajax({
            url: "product/export/productDictionaryExport?lx=" + lx,
            success: function (res) {
                if (res.status == 200) {
                    //配置列宽
                    let colConf = excel.makeColConfig({
                        'A': 80,
                        'X': 80
                    }, 80);
                    excel.exportExcel({
                        sheet1: res.data.data
                    }, '包材_颜色订货号.xlsx', 'xlsx', {
                        extend: {
                            '!cols': colConf
                        }
                    });
                } else {
                    layer.msg(res.msg, {icon: 5});
                }
                layer.close(index);
                return false;
            }
        });
        return false;
    }

//提交
    form.on('submit(submitform)', function (data) {
        data.field.id = id;
        data.field.lx = lx;
        $.ajax({
            type: 'POST',
            dataType: "json",
            contentType: 'application/json',
            data: JSON.stringify(data.field),
            url: 'product/category/add',
            async: false,
            success: function (res) {
                if (res.status == 200) {
                    table.reload('categoryTableId', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    }, 'data');
                    layer.closeAll(); //执行重载
                } else {
                    if (res.status == 11017) {
                        //编码已存在,需要生成新的编码
                        let flag = getNewCode();
                        if (flag) {
                            layer.msg("编码重复，已为您生成新的编码!", {icon: 6});
                        } else {
                            layer.msg("网络繁忙，请重新尝试！", {icon: 5});
                        }
                    } else {
                        layer.msg(res.msg, {icon: 5});
                    }
                }
            }
        });
        return false;
    })

//批量删除
    function batchDelete(ids) {
        let flag = false;
        $.ajax({
            url: "product/category/batchDelete?ids=" + ids,
            async: false,
            success: function (data) {
                if (data.status == 200) {
                    flag = true;
                    layer.msg('停用成功');
                } else {
                    layer.msg(data.msg);
                }
            }
        });
        return flag;
    }


//获取最新品类编码
    function getNewCode() {
        let flag = false;
        $.ajax({
            url: "product/getNewDictionaryCode?lx=" + lx,
            async: false,
            success: function (data) {
                if (data.status == 200) {
                    //颜色订货号："CON-" +3位有序数字
                    let code = data.data;
                    $("#code").val(code);
                    flag = true;
                } else {
                    layer.msg(data.msg, {icon: 5});
                }
            }
        });
        return flag;
    }

});